
<style scoped>
  .register{
    height:100%;
    background: #edf1f5;
  }
  .header{
    display: flex;
    flex-direction: column;
  }
  .head{
    height:60px;
    padding-top: 10px;
    display: flex;
    justify-content: center;
    align-items:center;
    border-bottom: 1px solid #E9E9E9;
    box-sizing: border-box;
    background: #ffffff;
    position: relative;
  }
  .head-left{
    position: absolute;
    width:40px;
    height:100%;
    left:0;
    top:0;
    color: #7f7a8e;
    text-align: center;
  }
  .head-left h1{
    margin-top: 22px;
  }
  .head-middle{
    font-size: 16px;
  }
  .head-right{
    position: absolute;
    right:10px;
    top:15px;
  }

  input{
    border:none;
  }
  .content1{
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 100%;
    margin-top: 80px;
    /*height:100%;*/
  }
  .content1>h3{
    margin-bottom: 20px;
  }

  .input{
    width:100%;
  }
  .input>div{
    margin:0 5%;
  }
  .input>div>.zhmm{
    background: #ffffff;
    border-radius: 10px;
  }
  .input>div>.zhmm>div{
    display: flex;
    align-items: center;
  }
  .input>div>.zhmm>div>label{
    flex: 2;
    display: flex;
    justify-content: center;
    align-items:center;
  }
  .input>div>.zhmm>div>input{
    flex: 7;
    display: flex;
    align-items: center;
    height:80%;
  }
  .input>div>.zhmm>div{
    border-bottom: 1px solid #e9e9e9;
    box-sizing: border-box;
  }
  .input>div>.zhmm>div input{
    padding-left: 10px;
  }
  .submit{
    margin-top: 30px;
    border-radius: 10px;
    background: #4D66FF;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .submit h3{
    color: #ffffff;
  }
  @media screen and (max-width: 320px) {
    .input>div>.zhmm>div{
      height:43px;
    }
    .submit{
      height:43px;
    }
    .input>div>.zhmm input{
      font-size: 1.2rem;
    }
  }
  @media screen and (max-width:375px) and (min-width:321px) {
    .input>div>.zhmm>div{
      height:50px;
    }
    .submit{
      height:50px;
    }
    .input>div>.zhmm input{
      font-size: 1.4rem;
    }
  }
  @media screen and (min-width: 376px){
    .input>div>.zhmm>div{
      height:60px;
    }
    .submit{
      height:60px;
    }
    .input>div>.zhmm input{
      font-size: 1.6rem;
    }
  }
  .confirm{
    display: flex;
    justify-content: center;
    margin-top: 8px;
  }

  /*验证*/
  .test{
    margin-left: 20px;
    margin-top: 4px;
    color: #e43937;
  }
</style>

<template>
  <div class="register" ref="nana">
    <div class="header">
      <div class="head-top"></div>
      <div class="head">
        <div class="head-left" @click="goBack"><h1 class="iconfont icon-xiangzuo1"></h1></div>
        <div class="head-middle"><h3>设置昵称</h3></div>
      </div>
    </div>

    <div class="content1">
      <h3>修改昵称</h3>
      <div class="input">
        <div>
          <div class="zhmm">
            <div class="name">
              <input type="text" placeholder="请输入昵称" v-model="name">
            </div>
            <small class="test" v-if="hasName">{{testName}}</small>
          </div>
          <div class="submit" @click="submit">
            <h3>确定</h3>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import axios from 'axios'
  import { MessageBox } from 'mint-ui'
  import host from '../config.js'
  import { Toast } from 'mint-ui'

  export default {
    data () {
      return {
        name: null,
        hasName: false,
        testName: null
      }
    },
    mounted () {
      let h = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight
      let h1 = h
      this.$refs.nana.style.height = h1 + 'px'
    },
    methods: {
      goBack () {
        this.$router.go(-1)
      },
      submit () {
        if(this.name === null) {
          this.hasName = true
          this.testName = '昵称不能为空'
          return
        }
        if(this.name.trim().length === 0){
          this.hasName = true
          this.testName = '昵称不能为空'
          return
        }
        let that = this
        let sendData = {
          "name": that.name.trim()
        }
        axios({
          method: 'post',
          url: host.host + "/api/usr/updateInfo",
          headers: {'Authorization': window.localStorage.getItem('token')},
          data: JSON.stringify(sendData)
        }).then(function (data) {
          if(data.data.code === 6006) {
            that.$router.push('../login')
          }
          if(data.data.code === 6007){
            that.$refreshToken()
          }
          if(data.data.code === 200){
            Toast({
              message: '操作成功',
              duration: 1000
            })
            let interval = window.setTimeout(function() {
              that.$router.push({'path':'/safe'})
            }, 1000)
          }
        })
      }
    }
  }
</script>
